//
//  DrawEarthViewController.m
//  OpenGLES_Learning
//
//  Created by Mac Center on 03/09/2013.
//  Copyright (c) 2013 Mac Center. All rights reserved.
//

#import "DrawEarthViewController.h"
#import "sphere.h"

@interface DrawEarthViewController ()

@end

@implementation DrawEarthViewController
@synthesize baseEffect;
@synthesize textureEarth;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    GLKView *glView = (GLKView *)self.view;
    NSAssert([glView isKindOfClass:[GLKView class]], @"There is no GLKView");
    glView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    
    glView.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    [EAGLContext setCurrentContext:glView.context];
    
    //config light
    self.baseEffect = [[GLKBaseEffect alloc] init];
    self.baseEffect.light0.enabled = GL_TRUE;
    self.baseEffect.light0.ambientColor = GLKVector4Make(0.2f, 0.2f, 0.2f, 1.0f);
    self.baseEffect.light0.diffuseColor = GLKVector4Make(0.7f, 0.7f, 0.7f, 1.0f);
    self.baseEffect.light0.position = GLKVector4Make(1.0, 0.0f, -0.8f, 0.0);
    
    CGImageRef earthImgRef = [UIImage imageNamed:@"Earth512x256.jpg"].CGImage;
    self.textureEarth = [GLKTextureLoader textureWithCGImage:earthImgRef options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil] error:NULL];
    self.baseEffect.texture2d0.name = self.textureEarth.name;
    self.baseEffect.texture2d0.target = self.textureEarth.target;

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    
    glGenBuffers(1, &vertexOfEarthId);
    glBindBuffer(GL_ARRAY_BUFFER, vertexOfEarthId);
    glBufferData(GL_ARRAY_BUFFER,sizeof(sphereVerts) , sphereVerts, GL_STATIC_DRAW);
    
    glGenBuffers(1, &normalOfEarthId);
    glBindBuffer(GL_ARRAY_BUFFER, normalOfEarthId);
    glBufferData(GL_ARRAY_BUFFER,sizeof(sphereNormals) , sphereNormals, GL_STATIC_DRAW);

    glGenBuffers(1, &textureOfEarthId);
    glBindBuffer(GL_ARRAY_BUFFER, textureOfEarthId);
    glBufferData(GL_ARRAY_BUFFER,sizeof(sphereTexCoords) , sphereTexCoords, GL_STATIC_DRAW);

    glEnable(GL_DEPTH_TEST);
}

- (void)viewDidUnload {
    GLKView *v = (GLKView *)self.view;
    [EAGLContext setCurrentContext:v.context];
    
    if (vertexOfEarthId != 0) {
        glDeleteBuffers(1, &vertexOfEarthId);
        vertexOfEarthId = 0;
    }
    if (normalOfEarthId != 0) {
        glDeleteBuffers(1, &normalOfEarthId);
        normalOfEarthId = 0;
    }
    if (textureOfEarthId != 0) {
        glDeleteBuffers(1, &textureOfEarthId);
        textureOfEarthId = 0;
    }
    
    v.context = nil;
    [EAGLContext setCurrentContext:nil];
}

- (void)dealloc {
    GLKView *v = (GLKView *)self.view;
    [EAGLContext setCurrentContext:v.context];
    
    if (vertexOfEarthId != 0) {
        glDeleteBuffers(1, &vertexOfEarthId);
        vertexOfEarthId = 0;
    }
    if (normalOfEarthId != 0) {
        glDeleteBuffers(1, &normalOfEarthId);
        normalOfEarthId = 0;
    }
    if (textureOfEarthId != 0) {
        glDeleteBuffers(1, &textureOfEarthId);
        textureOfEarthId = 0;
    }
    
    v.context = nil;
    [EAGLContext setCurrentContext:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    [self.baseEffect prepareToDraw];
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, vertexOfEarthId);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, (3 * sizeof(GLfloat)), NULL);

    glBindBuffer(GL_ARRAY_BUFFER, normalOfEarthId);
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, (3 * sizeof(GLfloat)), NULL);

    glBindBuffer(GL_ARRAY_BUFFER, textureOfEarthId);
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, (2 * sizeof(GLfloat)), NULL);
    
    glDrawArrays(GL_TRIANGLES, 0, sphereNumVerts);

}

@end
